package com.example.admintor.humaninteraction;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.wzsdk.login.Landing;
import com.example.wzsdk.login.LoginState;
import com.example.wzsdk.scan.Search;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Admintor on 2017/11/7.
 */

public class LoginActivity extends Activity implements View.OnClickListener, Search.SearchFinsh, MyAlerDialog.OnEditInputFinishedListener, LoginState {
    EditText ip, sn;
    Button login, search;
    Search searchs;
    public static Landing landing;
    MyAlerDialog myAlerDialog;
    private ProgressDialog progressDialog;


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    landing.AttenVarsByUiid(Data.GUAN_SHAN);
                    handler.sendEmptyMessageDelayed(0x456, 500);
                    break;
                case 0x456:
                    landing.AttenVarsByUiid(Data.REN_TI);
                    handler.sendEmptyMessageDelayed(0x789, 500);
                    break;
                case 0x789:
                    landing.AttenVarsByUiid(Data.YING_BING);
                    handler.sendEmptyMessageDelayed(0x159, 500);
                    break;
                case 0x159:
                    landing.AttenVarsByUiid(Data.YAN_WU);
                    handler.sendEmptyMessageDelayed(0x357, 500);
                    break;
                case 0x357:
                    landing.AttenVarsByUiid(Data.RAN_QI);
                    handler.sendEmptyMessageDelayed(0x845, 500);
                    break;
                case 0x845:
                    progressDialog.dismiss();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    break;
                case 0x888:
                    Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };
    SharedPreferences sp;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        FindView();
        Onclick();
        initSdk();
        sp = getSharedPreferences("ip_sn", Context.MODE_PRIVATE);
        myAlerDialog = new MyAlerDialog(this, list, this);
        SysApplication.getInstance().addActivity(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!sp.getString("ip", "null").equals("null")) {
            ip.setText(sp.getString("ip", ""));
            sn.setText(sp.getString("sn", ""));
        }
    }

    public void initSdk() {
        searchs = new Search("DFPMNAWAW");
        searchs.setSearchListener(this);
        landing = new Landing("DFPMNAWAW");
        landing.setLoginListener(this);
    }

    public void FindView() {
        ip = (EditText) findViewById(R.id.ip);
        sn = (EditText) findViewById(R.id.sn);
        login = (Button) findViewById(R.id.login);
        search = (Button) findViewById(R.id.search);
    }

    public void Onclick() {
        login.setOnClickListener(this);
        search.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                if (!ip.getText().toString().trim().equals("") && !sn.getText().toString().trim().equals("")) {
                    landing.Login(sn.getText().toString().trim(), ip.getText().toString().trim());
                    progressDialog = ProgressDialog.show(this, "系统提示", "正在加载中...", true, false);
                    sp.edit().putString("ip", ip.getText().toString().trim()).putString("sn", sn.getText().toString().trim()).commit();
                }
                break;
            case R.id.search:
                myAlerDialog.show();
                searchs.StartSearch(this);
                break;
        }
    }

    List<String[]> list = new ArrayList<>();

    @Override
    public void SearchFinsh(List<String[]> list) {
        if (list != null) {
            myAlerDialog.setData(list);
            myAlerDialog.refurbish();
        }
    }

    @Override
    public void editInputFinished(String ips, String serials, int number) {
        ip.setText(ips);
        sn.setText(serials);
    }

    @Override
    public void LoginStatus(boolean b, String s) {
        if (b) {
            handler.sendEmptyMessage(0x123);
        } else {
            handler.sendEmptyMessage(0x888);
        }
    }

    @Override
    public void AttentionStarting(String uiid, byte b, String index, String s2, String s3) {
        System.out.println(uiid + "  " + b + "  " + index + "  " + s2 + "  " + s3);
        switch (uiid) {
            case Data.GUAN_SHAN:
                Data.GUAN_SHAN_STR = b + "";
                break;
            case Data.REN_TI:
                Data.REN_TI_STR = b + "";
                break;
            case Data.YING_BING:
                Data.YING_BING_STR = b + "";
                break;
            case Data.YAN_WU:
                Data.YAN_WU_STR = b + "";
                break;
            case Data.RAN_QI:
                Data.RAN_QI_STR = b + "";
                break;
        }
    }

    @Override
    public void AttentionProcess(String uiid, byte b, String s1, String index, String s3, String s4) {
        System.out.println(uiid + "  " + b + "  " + s1 + "  " + index + "  " + s3 + "  " + s4);
        switch (uiid) {
            case Data.GUAN_SHAN:
                Data.GUAN_SHAN_STR = b + "";
                break;
            case Data.REN_TI:
                Data.REN_TI_STR = b + "";
                break;
            case Data.YING_BING:
                Data.YING_BING_STR = b + "";
                break;
            case Data.YAN_WU:
                Data.YAN_WU_STR = b + "";
                break;
            case Data.RAN_QI:
                Data.RAN_QI_STR = b + "";
                break;
        }
    }

    @Override
    public void SetVarState(String s, byte b, String s1) {

    }

    @Override
    public void SetKeyState(String s, String s1, String s2) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        landing.exitLogin();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            new AlertDialog.Builder(this).setTitle("系统提示").setMessage("是否要退出？").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    landing.exitLogin();
                    SysApplication.getInstance().exit();
                }
            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }).show();
        }
        return super.onKeyDown(keyCode, event);
    }
}
